package com.hzxy.yiyid.web.tool.baidu;

import com.hzxy.yiyid.web.tool.baidu.result.AddressResult;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @ClassName BaiduInfoConverter
 * @Description 百度返回信息解析
 * @Author RAICOM2023
 * @Date 2023/5/4 20:35
 * @Version 1.0
 **/
public class BaiduInfoConverter {

    /*
     * @Author RAICOM2023
     * @Description //转换地址结果
     * @Date 15:43 2023/3/6
     * @Param [json]
     * @return com.hzxy.yiyid.web.tool.baidu.result.AddressResult
     **/
    public static AddressResult convertAddressResult(JSONObject json){
        AddressResult addressResult = new AddressResult();
        addressResult.setProvince(json.has("province") ? json.getString("province") : null);
        addressResult.setProvinceCode(json.has("province_code") ? json.getString("province_code") : null);
        addressResult.setCity(json.has("city") ? json.getString("city") : null);
        addressResult.setCityCode(json.has("city_code") ? json.getString("city_code") : null);
        addressResult.setCounty(json.has("county") ? json.getString("county") : null);
        addressResult.setCountyCode(json.has("county_code") ? json.getString("county_code") : null);
        addressResult.setTown(json.has("town") ? json.getString("town") : null);
        addressResult.setTownCode(json.has("town_code") ? json.getString("town_code") : null);
        addressResult.setDetail(json.has("detail") ? json.getString("detail") : null);
        try{
            addressResult.setLng(json.has("lng") ? json.getDouble("lng") : null);
            addressResult.setLat(json.has("lat") ? json.getDouble("lat") : null);
        }catch(Exception e){
            //do nothing
        }
        return addressResult;
    }

    public static String getOcrString(JSONObject res) throws Exception {
        StringBuilder sb = new StringBuilder();
        JSONArray array = res.getJSONArray("words_result");
        for(int i=0;i<array.length();i++){
            sb.append(array.getJSONObject(i).getString("words")).append(",");
        }
        return sb.toString();
    }

}
